﻿//-----------------------------------------------------------------------
// <copyright file="Edge.cs" company="Luboš Bretschnieder">
// Copyright (c) 2008 Luboš Bretschnieder, Licensed under the Apache License, Version 2.0.
// </copyright>
//-----------------------------------------------------------------------
namespace Graphs
{
    using System;

    /// <summary>
    /// Edge implementation.
    /// </summary>
    public class Edge : ICloneable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Edge"/> class.
        /// </summary>
        /// <param name="from">From - start vertex.</param>
        /// <param name="to">To - end vertex.</param>
        public Edge(Vertex from, Vertex to)
        {
            this.From = from;
            this.To = to;
            this.State = EdgeState.Normal;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Edge"/> class.
        /// </summary>
        /// <param name="score">The edge score.</param>
        /// <param name="from">From - start vertex.</param>
        /// <param name="to">To - end vertex.</param>
        public Edge(int score, Vertex from, Vertex to) : this(from, to)
        {
            this.Score = score;
        }

        /// <summary>
        /// Gets or sets the score.
        /// </summary>
        /// <value>The score.</value>
        public int Score { get; set; }

        /// <summary>Gets or sets the start vertex. </summary>
        public Vertex From { get; set; }

        /// <summary>Gets or sets the end vertex.</summary>
        public Vertex To { get; set; }

        /// <summary>Gets or sets the state of edge.</summary>
        public EdgeState State { get; set; }

        /// <summary>
        /// Checks if edge connects specified vertices.
        /// </summary>
        /// <param name="v1">The vertex 1.</param>
        /// <param name="v2">The vertex 2.</param>
        /// <returns>true if vertices are connected by this edge, otherwise false</returns>
        public bool Connects(Vertex v1, Vertex v2)
        {
            if (this.From.Name == v1.Name && this.To.Name == v2.Name)
            {
                return true;
            }

            if (this.From.Name == v2.Name && this.To.Name == v1.Name)
            {
                return true;
            }

            return false;
        }

        /// <summary>Creates a new object that is a copy of the current instance.</summary>
        /// <returns>A new object that is a copy of this instance.</returns>
        object ICloneable.Clone()
        {
            return this.Clone();
        }

        /// <summary>Clones this instance.</summary>
        /// <returns>The cloned edge.</returns>
        public virtual Edge Clone()
        {
            return (Edge)MemberwiseClone();
        }
    }
}